package day18;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author: 王丹
 * @create: 2021-06-17 15:01
 * 线程池使用的简单示例
 **/
public class Demo1 {
    /**
     *
     */
    static ThreadPoolExecutor executor =
            new ThreadPoolExecutor(3, 5, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());

    public static void main(String[] args) {
        for (int i = 0; i < 8; i++) {
            int j = i;
            /*executor.execute(() -> {
                try {
                    //模拟业务逻辑耗时
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //Variable used in lambda expression should be final or effectively final
                System.out.println(Thread.currentThread().getName() + "当前任务" + j);
            });*/
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        //模拟业务逻辑耗时
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //Variable used in lambda expression should be final or effectively final
                    System.out.println(Thread.currentThread().getName() + "new 当前任务" + j);
                }
            });
        }
        executor.shutdown();
    }
}
